package com.github.xuemingyu.springboot.acticitycloud.controller;

import com.bidanet.bdcms.core.bean.ApiResult;
import com.bidanet.bdcms.core.common.SpringWebTool;
import com.bidanet.bdcms.core.controller.BaseController;
import com.github.xuemingyu.springboot.acticitycloud.driver.file.FileDrive;
import com.github.xuemingyu.springboot.acticitycloud.driver.file.FileUploadMessage;
import com.github.xuemingyu.springboot.acticitycloud.driver.file.LocalFileDrive;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by yf003 on 2017/7/17.
 */
@Controller
@RequestMapping("/public/file")
public class FileController extends BaseController {

    /**
     * 上传文件
     *
     * @param files
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ApiResult<List<String>> upload(@RequestParam("file") CommonsMultipartFile[] files) {
        try {
            ArrayList<String> fileUrls = new ArrayList<>();
            for (CommonsMultipartFile file : files) {
                FileDrive fileDrive = ContextLoader.getCurrentWebApplicationContext().getBean("qiNiuFileDrive", FileDrive.class);

                String upload = fileDrive.uploadImg(file.getInputStream(), file.getOriginalFilename());

                fileUrls.add(upload);
            }
            return ApiResult.success(fileUrls);
        } catch (IOException e) {
            return ApiResult.error("上传失败");
        }
    }


    /**
     * 上传图片
     *
     * @param files
     * @return
     */
    @RequestMapping("/uploadImg")
    @ResponseBody
    public FileUploadMessage uploadImg(@RequestParam("file") MultipartFile[] files) {
        FileUploadMessage msg = new FileUploadMessage();
        if (files.length > 0) {
            try {
                MultipartFile file = files[0];
                FileDrive fileDrive = SpringWebTool.getBean(LocalFileDrive.class);
                String upload = fileDrive.uploadImg(file.getInputStream(), file.getOriginalFilename(), "wkjy_");
                String substring = upload.substring(7).replaceAll("\\\\", "/");
                // msg.setUrl(substring);
                msg.setUrl(upload.replaceAll("\\\\", "/"));
                msg.setError(FileUploadMessage.CODE_SUCCESS);

            } catch (IOException e) {
                msg.setError(FileUploadMessage.CODE_ERROR);
                msg.setMessage(e.getMessage());
            }
        } else {
            msg.setError(FileUploadMessage.CODE_ERROR);
            msg.setMessage("文件不存在");
        }
        return msg;
    }

    @RequestMapping("/editUploadImg")
    @ResponseBody
    public FileUploadMessage editUploadImg(@RequestParam("imgFile") MultipartFile[] files) {
        FileUploadMessage msg = new FileUploadMessage();
        if (files.length > 0) {
            try {
                MultipartFile file = files[0];
                FileDrive fileDrive = SpringWebTool.getBean(LocalFileDrive.class);
                String upload = fileDrive.uploadImg(file.getInputStream(), file.getOriginalFilename(), "wkjy_");
                msg.setUrl(upload);
                msg.setError(FileUploadMessage.CODE_SUCCESS);
            } catch (IOException e) {
                msg.setError(FileUploadMessage.CODE_ERROR);
                msg.setMessage(e.getMessage());
            }
        } else {
            msg.setError(FileUploadMessage.CODE_ERROR);
            msg.setMessage("文件不存在");
        }
        return msg;
    }
}
